<form class="layui-form" action="">


    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" required  placeholder="请输入标题" autocomplete="off"
                class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">来源</label>
        <div class="layui-input-block">
            <input type="text" name="source" required  placeholder="请输入来源" autocomplete="off"
                class="layui-input">
        </div>
    </div>
    

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea id="demo" style="display: none;"></textarea>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    //Demo
    layui.use(['form', 'jquery', 'laydate', 'layedit', 'layer'], function () {

        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layedit = layui.layedit;
        var layer = layui.layer;

        //配置编辑器
        layedit.set({
            uploadImage: {
                url: '/admin/upload' //接口url
                , type: '' //默认post
            }
        });

        //建立编辑器
        var index = layedit.build('demo');

      
        //监听提交
        form.on('submit(formDemo)', function (data) {

            try {
                //初始化
                let tempArr = []

                //获取编辑器内容
                let content = layedit.getContent(index)

                //其他表单值
                let { title, source} = data.field

                //ajax
                
                $.post("/admin/newsAdd-do", { content, title, source}, (res) => {

                    if (res == 'success') {

                        //提示
                        layer.msg("数据已保存！")

                        //重置富文本编辑器
                        index = layedit.build('demo');

                        //重置表单
                        $("form")[0].reset()

                    } else {
                        //提示
                        layer.msg("数据保存失败！")
                    }

                })
              

                //阻止表单提交
                return false;

            } catch (err) {

                console.log(err);
                //阻止表单提交
                return false;
            }

        });

        //重置表单
        form.render()

    });
</script>